import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SlidingWindow {
    public String minWindow(String s, String t) {
        Map<Character, Integer> window = new HashMap<>();
        Map<Character, Integer> need = new HashMap<>();
        for (int i = 0; i < t.length(); i++) {
            char c = t.charAt(i);
            need.put(c, need.getOrDefault(c, 0) + 1);
        }
        int left = 0, right = 0, valid = 0;
        int start = 0, len = Integer.MAX_VALUE;
        while (right < s.length()) {
            char ch = s.charAt(right++);
            if (need.getOrDefault(ch, 0) > 0) {
                window.put(ch, window.getOrDefault(ch, 0) + 1);
                if (window.get(ch).equals(need.get(ch))) {
                    ++valid;
                }
            }

            while (valid == need.size()) {
                if (right - left < len) {
                    start = left;
                    len = right - left;
                }
                char leftChar = s.charAt(left++);
                if (need.getOrDefault(leftChar, 0) > 0) {
                    if (window.getOrDefault(leftChar, 0).equals(need.get(leftChar))) {
                        --valid;
                    }
                    window.put(leftChar, window.getOrDefault(leftChar, 0) - 1);
                }
            }
        }
        return len == Integer.MAX_VALUE ? "" : s.substring(start, start + len);
    }

    public boolean checkInclusion(String s1, String s2) {
        Map<Character, Integer> window = new HashMap<>();
        Map<Character, Integer> need = new HashMap<>();
        for (int i = 0; i < s1.length(); i++) {
            char c = s1.charAt(i);
            need.put(c, need.getOrDefault(c, 0) + 1);
        }
        int left = 0, right = 0, valid = 0;
        while (right < s2.length()) {
            char ch = s2.charAt(right++);
            if (need.getOrDefault(ch, 0) > 0) {
                window.put(ch, window.getOrDefault(ch, 0) + 1);
                if (window.get(ch).equals(need.get(ch))) {
                    ++valid;
                }
            }

            while (right - left >= s1.length()) {
                if (valid == need.size()) {
                    return true;
                }
                char leftChar = s2.charAt(left++);
                if (need.getOrDefault(leftChar, 0) > 0) {
                    if (window.getOrDefault(leftChar, 0).equals(need.get(leftChar))) {
                        --valid;
                    }
                    window.put(leftChar, window.getOrDefault(leftChar, 0) - 1);
                }
            }
        }
        return false;
    }

    public List<Integer> findAnagrams(String s, String p) {
        Map<Character, Integer> window = new HashMap<>();
        Map<Character, Integer> need = new HashMap<>();
        for (int i = 0; i < p.length(); i++) {
            char c = p.charAt(i);
            need.put(c, need.getOrDefault(c, 0) + 1);
        }
        int left = 0, right = 0, valid = 0;
        List<Integer> res = new ArrayList<>();
        while (right < s.length()) {
            char ch = s.charAt(right++);
            if (need.getOrDefault(ch, 0) > 0) {
                window.put(ch, window.getOrDefault(ch, 0) + 1);
                if (window.get(ch).equals(need.get(ch))) {
                    ++valid;
                }
            }

            while (right - left >= p.length()) {
                if (valid == need.size()) {
                    res.add(left);
                }
                char leftChar = s.charAt(left++);
                if (need.getOrDefault(leftChar, 0) > 0) {
                    if (window.getOrDefault(leftChar, 0).equals(need.get(leftChar))) {
                        --valid;
                    }
                    window.put(leftChar, window.getOrDefault(leftChar, 0) - 1);
                }
            }
        }
        return res;
    }
}
